/*** Created by yun on 2017/12/8.*/
//转换时间戳
app.service('getTime',function(){
    this.getLocalTime= function(fmt) {
        return new Date(parseInt(fmt)).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ").substr(0,19);
    };
   function add(m){return m<10?'0'+m:m }
    Date.prototype.toLocaleString = function() {
        var y = this.getFullYear();
        var m =this.getMonth()+1;
        var d = this.getDate();
        var h = this.getHours();
        var mm = this.getMinutes();
        var s = this.getSeconds();
        return y+'-'+add(m)+'-'+add(d)+' '+add(h)+':'+add(mm)+':'+add(s);
    };
    this.bytesToSize=function(bytes) {
        if (bytes ==0) return '0 B';
        var k = 1024, // or 1024
            sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
            i = Math.floor(Math.log(bytes) / Math.log(k));
        return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i];
    }
});
//获取存储空间
app.service('getStorage',function($http,$sessionStorage){
    function bytesToSize(bytes) {
        if (bytes == 0) return '0 B';
        var k = 1024, // or 1024
            sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
            i = Math.floor(Math.log(bytes) / Math.log(k));
        return (bytes / Math.pow(k, i)).toFixed(0) + ' ' + sizes[i];
    }
    this.newStorage=function(){
        var user = angular.fromJson($sessionStorage.USER);
        $http({
            method: 'post',
            url:Url+'filemanager/filePercentage?tenanid='+user.tenanid+'',
            withCredentials:true,
            headers:{
                Authorization:token
            },
        }).then(function successCallback(response) {
            if(response.data.code==0){
                var storage=bytesToSize(response.data.data.storage);
                var totalSize= bytesToSize(response.data.data.totalSize);
                var sizepercentage=response.data.data.percentage;
                $('#progress1').empty();
                var html='<div class="progress"><div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="'+sizepercentage+'" aria-valuemin="0" aria-valuemax="100" style="width: '+sizepercentage+'" >'+sizepercentage+'</div> </div> <div> <p> <span id="totalsize">'+totalSize+'/</span> <span id="storage">'+storage+'</span> </p> </div>'
                $('#progress1').append(html);
            }else{
                toastr.error(response.data.message);
            }
        },function errorCallback(response){
            toastr.error(response.data.message);
        });
    };

});

